$(document).ready(function() {
   // Datepicker js
   $(document).on('focus','#from',function(){
      $(this).datepicker({
         dateFormat: 'yy-mm-dd',
         changeYear: true,
         changeMonth: true,
         numberOfMonths: 1,
         minDate: new Date(),
         onClose: function(dateText) {
            if(dateText != $(this).attr("title"))
               $(this).removeClass("blur");
            else
               $(this).blur();
         }
      });
   });

   // Timepicker js
   $(document).on('focus','#fromtime',function(){
      if($(this).data('timepicker-settings'))
         return true;

      $(this).timepicker({
         timeFormat: 'H:i',
         step: '30',
         minTime: '07:00',
         maxTime: '21:00'
      });
   });

   $("#rejectform").on("click", "#reject_confirm", function(){
      $("#rejectconfirmdialog").modal();
   });

   $("#rejectform").validate({
      rules: {
         reason: "required"
      },
      messages: {
         reason: "请选择放弃原因"
      },
      errorPlacement: function(error, element) {
         error.appendTo('.server');
      },
      invalidHandler:function(form){
         $("#rejectconfirmdialog").modal('hide');
      },
      submitHandler: function(form) {
         $("#rejectform").ajaxSubmit({
            type: 'post',
            dataType: "json",
               beforeSubmit: function(data, form, options) {
                  if (!showLoading("#reject_submit", options, null, {button:true}))
                     return false;
                  $(".server").hide();
               },
               success: function(data) {
                  try {
                     var retmsg = data;
                     if (retmsg.error) {
                        $("#rejectconfirmdialog").modal('hide');
                        $(".server").jobUtil('showError', retmsg.error);
                     }
                     else if(retmsg.success) {
                        $("#rejectconfirmdialog").modal('hide');
                        $(".server", form).jobUtil('showSuccess', retmsg.success, null);
                        $(".closewin", form).hide();
                        setTimeout(function() { window.location.reload(); }, 1000);
                     }
                  } catch(e) {
                     return false;
                  }
               }
         });
      }
   });

   //reject offer

   $("#rejectofferform").on("click", "#reject_offer_confirm", function(){
      $("#rejectofferconfirmdialog").modal();
   });
   
  $("#rejectofferform").validate({
      errorPlacement: function(error, element) {
         error.appendTo('.server');
      },
      invalidHandler:function(form){
         $("#rejectofferconfirmdialog").modal('hide');
      },
      submitHandler: function(form) {
         $("#rejectofferform").ajaxSubmit({
            type: 'post',
            dataType: "json",
               beforeSubmit: function(data, form, options) {
                  if(!showLoading("#reject_offer_submit", options, null, {button:true}))
                     return false;
                  $(".server").hide();
               },
               success: function(data) {
                  try {
                     var retmsg = data;
                     if(retmsg.error) {
                        $("#rejectofferconfirmdialog").modal('hide');
                        $(".server").jobUtil('showError', retmsg.error);
                     }
                     else if(retmsg.success) {
                        $("#rejectofferconfirmdialog").modal('hide');
                        $(".server", form).jobUtil('showSuccess', retmsg.success, null);
                        $(".closewin", form).hide();
                        setTimeout(function() { window.location.reload(); }, 1000);
                     }
                  } catch(e) {
                     return false;
                  }
               }
         });
      }
   });
});
